<?php 
## include required files
/*******************************/
require_once '../model/faqs.php';
require_once '../model/moduleuser.php';
## Paginaton class
require_once ("../includes/classes/pagination_class.php");
/*******************************/
## Create Objects
/*******************************/
$faqObj = new Model_FAQ();
$moduleObj=new Model_ModuleUser();
/*******************************/

## Active, Inactive selected records
/*******************************/
if(isset($_POST['go']) != '' && isset($_POST['action']) != '') {
		$ids = @implode(",", $_POST['checkall']);
		## check which action is selected
		$hidden_page = $_POST['hidden_pageno'];	
		if($_POST['action'] == 'active') {
			## Active selected records
			$faqObj->updateFaqCategoryStatus($ids, '1');
			$_SESSION['msg']="<div class='success_msg'><span>FAQ status activated successfully</span></div>";
		} else if($_POST['action'] == 'inactive') {
			## Inactive selected records
			$faqObj->updateFaqCategoryStatus($ids, '0');
			$_SESSION['msg']="<div class='success_msg'><span>FAQ status deactivated successfully</span></div>";
		} else {
			## Delete selected records
			$faqObj->deleteFaqCategory($ids);
			$_SESSION['msg']="<div class='success_msg'><span>FAQ deleted successfully</span></div>";
		}
		header('location:'.SITE_URL.'/admin/home.php?q=faqcategories&page='.$hidden_page);
			exit;
		## Delete code will be here if required
		
} // if close

## Active/Inactive/Delete thourgh URL
if(isset($_GET['action']) == 'status' && isset($_GET['status']) != '') {
	$id = base64_decode($_GET['id']);
	## Update cms page status 
	$array = array();
	$array['status'] = ($_GET['status']=='1'?0:1);
	$faqObj->editFaqCategoryById($array, $id);
	$_SESSION['msg']="<div class='success_msg'><span>FAQ status updated successfully</span></div>";
	header('location:'.SITE_URL.'/admin/home.php?q=faqcategories&page='.$_GET['page']);
	exit;
}

if(isset($_GET['action']) == 'delete' && isset($_GET['id']) != '') {
	$id = base64_decode($_GET['id']);
	## Delete FAQ category
	$faqObj->deleteFaqCategoryById($id);
	$_SESSION['msg']="<div class='success_msg'><span>FAQ category deleted successfully</span></div>";
	header('location:'.SITE_URL.'/admin/home.php?q=faqcategories&page='.$_GET['page']);
	exit;
}
if($_GET['q']=='faqcategories')
{
	$moduelArray=$moduleObj->getModuleHelpText(3);
	$smarty->assign('moduelArray', $moduelArray);
	//echo '<pre>';print_r($moduelArray);exit;
}

/*******************************/
##Fetch all cms pages from database
/******************************/
## Get search parameters in variables - 
if($_POST['search'] != '' && $_POST['search'] != 'Search') {
	$searchindex = $_POST['search'];
} else if($_GET['search'] != '') {
	$searchindex = $_GET['search'];
} else {
	$searchindex = '';
}
$searchindex=trim($searchindex);
// Assign search variable to show in search textbox
$smarty->assign('search', $searchindex);

##  --------- Pagination part first start --------------##
	if( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] != "" ) {
		$pageNum = $_GET[ 'page' ];
	} else {
		$pageNum = 1;
	} 
	$rowsPerPage = ROW_PER_PAGE;
	$pageName = "home.php?q=faqcategories"; 
	## Count all the records
	$cmsArray = $faqObj->getAllFaqsCategory($searchindex);
	$total_rows = count($cmsArray);
	$offset	= ($pageNum - 1) * $rowsPerPage;
	$smarty->assign('page', $pageNum);
##  --------- Pagination part first end --------------##


##  --------- Pagination part second start --------------##
	$newid = $pageNum * $rowsPerPage - $rowsPerPage + 1;
	$smarty->assign('newid', $newid);
	$faqArray = $faqObj->getAllFaqsCategory($searchindex, $rowsPerPage,$offset);
	//echo "<pre>"; print_r($cmsArray);exit;
	$smarty->assign('faqArray', $faqArray);
	
	if($searchindex != '') {
		$other_id = "search=".$searchindex;
	} else {
		$other_id = '';
	}
	
	if($total_rows > ROW_PER_PAGE) {
		$pg = new pagination();					// CREATING OBJECT OF THE PAGINATION CLASS
		$pgnation = $pg->pagination_admin( $total_rows , $rowsPerPage , $pageNum , $pageName , $other_id, $class);	
		$smarty-> assign("pagination",$pgnation);
	}
##  --------- Pagination part second end --------------##	

## Assign session message to smarty variable and unset session variable
if($_SESSION['msg'] != '') {
	$smarty->assign('msg', $_SESSION['msg']);	
	unset($_SESSION['msg']);
}

## Set active class variable for left menu  (main menu and sub menu)
$smarty->assign('activeclass', 'faqs_cats');
$smarty->assign('mainmenu', '2');

## Unset all the objects created which are on this page
unset($faqObj);
unset($moduleObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'faqcategories/faqcategories.tpl');
unset($smarty);
?>